package cn.com.djin.ssm.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;

import cn.com.djin.ssm.entity.User;
import cn.com.djin.ssm.util.SmsDemo;
import cn.com.djin.ssm.util.TencentSMS;
import cn.com.djin.ssm.util.UploadFile;
import cn.com.djin.ssm.util.VerifyCodeUtils;

/**
 * 
 * @author djin 用户控制器层
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User> {

	// 获取动态验证码
	@RequestMapping("/getVerifyCode")
	public void getVerifyCode(HttpSession session, HttpServletResponse response) {
		// 通过工具获取动态验证码
		String verifyCode = VerifyCodeUtils.generateVerifyCode(5);
		// 将动态验证码装入到HttpSession容器中
		session.setAttribute("verifyCode", verifyCode.toLowerCase());
		try {
			// 将动态验证码通过响应输出流对象写入到后台登录页面中
			VerifyCodeUtils.outputImage(250, 35, response.getOutputStream(), verifyCode);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 验证动态验证码
	@RequestMapping("/yzVerifyCode")
	@ResponseBody
	public String yzVerifyCode(String userVerifyCode, HttpSession session) {
		if (userVerifyCode.toLowerCase().equals(session.getAttribute("verifyCode"))) {
			return SUCCESS;
		} else {
			return FAIL;
		}
	}

	// 后台登录
	@RequestMapping("/loginht")
	@ResponseBody
	public String loginht(User user, HttpSession session) {
		try {
			User htUser = baseService.findTByPramas(user);
			if (htUser != null) {
				if (htUser.getUsertype().getUtid() == 103) {
					// 将登录的对象装进session容器中
					session.setAttribute("htUser", htUser);
					return SUCCESS;
				} else {
					return HTJURISDICTION;
				}
			} else {
				return FAIL;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	// 前台退出
	@RequestMapping("/qtExit")
	@ResponseBody
	public String qtExit(HttpSession session) {
		session.removeAttribute("loginQtUser");
		return SUCCESS;
	}

	// 后台退出
	@RequestMapping("/htExit")
	@ResponseBody
	public String htExit(HttpSession session) {
		session.removeAttribute("htUser");
		return SUCCESS;
	}

	// 用户头像上传
	@RequestMapping("/uploadUserImg")
	@ResponseBody
	public Object uploadImg(MultipartFile avater, String path) {
		try {
			map.put("uploadImgIf", UploadFile.upload(avater, path));
		} catch (IOException e) {
			e.printStackTrace();
			map.put("uploadImgIf", ERROR);
		}
		return map;
	}

	// 验证用户字段是否存在
	@RequestMapping("/checkRegColumn")
	@ResponseBody
	public Map<String, Object> checkRegColumn(User user) {
		try {
			if (baseService.findTotalByPramas(user) > 0) {
				map.put("valid", false);
			} else {
				map.put("valid", true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	// 验证pohne字段是否存在
	@RequestMapping("/checkPhone")
	@ResponseBody
	public String checkPhone(User user) {

		try {
			baseService.findTotalByPramas(user);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}

	}

	// 短信发送
	@RequestMapping("/sendCode")
	@ResponseBody
	public String sendCode(String code, String phone) {
		String params[] = { code };
		String phoneNumbers[] = { phone };
		return TencentSMS.sendingSMS(phoneNumbers, params);
	}

	// 前台登录
	@RequestMapping("/loginqt")
	@ResponseBody
	public String loginqt(User user, HttpSession session) {
		try {
			User loginQtUser = baseService.findTByPramas(user);

			if (loginQtUser != null) {
				if (loginQtUser.getUsertype().getUtid() != 103) {

					session.setAttribute("loginQtUser", loginQtUser);
					System.out.println(loginQtUser.getUid() + "登录id");

					return SUCCESS;
				} else {
					return null; // 这里返回什么？

				}
			} else {
				return FAIL;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

}
